package com.gy.assistant.ui.chat.group.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import im.zego.zim.entity.ZIMGroupMemberInfo;
import com.gy.assistant.R;
import com.gy.assistant.enums.GroupRoleType;
import com.gy.assistant.util.GlideUtils;
import java.util.ArrayList;
import java.util.List;

public class GroupMemberSearchAdapter extends RecyclerView.Adapter {

    private List<ZIMGroupMemberInfo> memberInfoList = new ArrayList<>();
    private OnItemClickListener onItemClickListener;
    private GroupRoleType groupRole;

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder viewHolder = new GroupMemberInfoHolder(
            LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_search_group_member_info, parent, false));
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        GroupMemberInfoHolder infoHolder = (GroupMemberInfoHolder)holder;
        infoHolder.setMemberInfo(memberInfoList.get(position));
    }

    @Override
    public int getItemCount() {
        return memberInfoList.size();
    }

    public void addOrUpdate(ZIMGroupMemberInfo newO) {
        for (int i = 0; i < memberInfoList.size(); i++) {
            ZIMGroupMemberInfo oldO = memberInfoList.get(i);
            if (newO.userID.equals(oldO.userID)) {
                memberInfoList.set(i, newO);
                return;
            }
        }
        memberInfoList.add(newO);
    }

    public void delete(ZIMGroupMemberInfo delO) {
        for (ZIMGroupMemberInfo oldO : memberInfoList) {
            if (delO.userID.equals(oldO.userID)) {
                memberInfoList.remove(oldO);
                return;
            }
        }
    }

    public void setGroupRole(GroupRoleType role) { this.groupRole = role; }

    public interface OnItemClickListener {
        void onItemClick(GroupMemberUpdateType type);

        void onItemInfoClick(ZIMGroupMemberInfo info);
    }

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.onItemClickListener = mOnItemClickListener;
    }

    protected class GroupMemberInfoHolder extends RecyclerView.ViewHolder {

        public TextView titleTextView;
        public ImageView headerImageVIew;
        public RelativeLayout relativeLayout;

        public GroupMemberInfoHolder(@NonNull View itemView) {
            super(itemView);
            titleTextView = itemView.findViewById(R.id.title);
            headerImageVIew = itemView.findViewById(R.id.header);
            relativeLayout = itemView.findViewById(R.id.relativeLayout);
        }

        public void setMemberInfo(ZIMGroupMemberInfo info) {
            String title = info.memberNickname;
            if (info.memberNickname != null && !info.memberNickname.equals("")) {
                title = info.memberNickname;
            } else if (info.userName != null && !info.userName.equals("")) {
                title = info.userName;
            } else if (info.userID != null) {
                title = info.userID;
            }
            titleTextView.setText(title);
            GlideUtils.loadUrl(headerImageVIew, info.memberAvatarUrl);
        }
    }

    public enum GroupMemberUpdateType {
        GROUP_MEMBER_UPDATE_TYPE_ADD,
        GROUP_MEMBER_UPDATE_TYPE_Delete
    }
}
